package com.nova.scenic.projectlibs.util.resloader;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;

import com.nova.scenic.projectlibs.util.debug.MyLog;

import java.util.List;

/**
 * Created by scenic on 3/27/15.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public abstract class AsyncTaskLoader<T extends List> extends android.content.AsyncTaskLoader<T> {

    private static final String TAG = "AsyncTaskLoader";

    public boolean isLoadingComplete;

    /**
     * some time , it a List
     */
    private T mData;

    public AsyncTaskLoader(Context context) {
        super(context);
    }


    @Override
    protected void onStartLoading() {
        isLoadingComplete = false;

        int dataSize = 0;
        if (mData != null) {
            deliverResult(mData);
            dataSize = mData.size();
        }
        boolean contentChanged = takeContentChanged();

        MyLog.i(TAG, getName() +
                "onStartLoading dataSize " + dataSize +
                " contentChanged " + contentChanged +
                " mData == null " + (mData == null));

        if (contentChanged || mData == null) {
            forceLoad();
        }

    }

    @Override
    protected void onForceLoad() {
        MyLog.i(TAG, getName() + "onForceLoad ");
        super.onForceLoad();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected boolean onCancelLoad() {
        MyLog.i(TAG, getName() + "onCancelLoad ");
        return super.onCancelLoad();
    }

    @Override
    protected T onLoadInBackground() {
        MyLog.i(TAG, getName() + "onLoadInBackground start");
        long time = System.currentTimeMillis();
        T value = super.onLoadInBackground();
        MyLog.i(TAG, getName() + "onLoadInBackground end time " + (System.currentTimeMillis() - time));
        return value;
    }

    @Override
    public void deliverResult(T data) {
        MyLog.i(TAG, getName() + "isReset " + isReset() + " isStarted() " + isStarted());

        if (isReset()) {
            onReleaseResources(data);
            return;
        }

        T oldData = mData;
        mData = data;

        if (isStarted())
            super.deliverResult(data);

        if (oldData != null && oldData != data)
            onReleaseResources(oldData);

        isLoadingComplete = true;
    }

    @Override
    protected void onStopLoading() {
        MyLog.i(TAG, getName() + "onStopLoading ");
//        cancelLoad();
    }

    @Override
    protected void onReset() {
        MyLog.i(TAG, getName() + "onReset ");
//        onStopLoading();
        if (mData != null) {
            onReleaseResources(mData);
            mData.clear();
            mData = null;
        }
    }

    public void onCanceled(T data) {
        MyLog.i(TAG, getName() + "onCanceled " + data);
        onReleaseResources(data);
    }

    protected void onReleaseResources(T data) {
//        MyLog.i(TAG,getName() + "onReleaseResources ");
    }

    public String getName() {
        return MyLog.getInstanceName(this);
    }

    public boolean isLoadingComplete() {
        return this.isLoadingComplete;
    }

}
